	//
	//  GaoCvImageLabelCell.m
	//  OpenglDemo
	//
	//  Created by Gikki Ares on 2020/6/24.
	//



#import "JsCvcIl.h"

@interface Prefix(CvcIl)()
{
	UILabel *mlbl_title;
	UIImageView *miv;
	UIImage * mimage_selected;
	UIImage * mimage_unselected;
}

@end

@implementation Prefix(CvcIl)

- (instancetype)init
{
	self = [super init];
	if (self) {
		[self commonInit];
	}
	return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
	self = [super initWithFrame:frame];
	if (self) {
		[self commonInit];
	}
	return self;
}

- (void)awakeFromNib {
	[super awakeFromNib];
		// Initialization code
	[self commonInit];
	
}


- (void) commonInit {
	miv = [[UIImageView alloc]init];
	[self addSubview:miv];
	mlbl_title = [[UILabel alloc]init];
	[self addSubview:mlbl_title];
	mlbl_title.textAlignment = NSTextAlignmentCenter;
}


- (void)displayModel:(Prefix(CvcIlModel) *)model {
	
		//只有标题
	if(model->mstr_title.length&&!model->mstr_imageName_selected.length) {
		mlbl_title.text = model->mstr_title;
		
			//计算lable的位置,居中
		mlbl_title.frame = self.bounds;
		mlbl_title.textColor = [UIColor blackColor];
		mlbl_title.font = model->mstyle.titleFont;
		mlbl_title.numberOfLines = 0;
	}
		//有标题和图片
	else {
			//标题的高度固定, space总共0.3,title0.3,image 0.4
		CGFloat mf_height_cell = self.bounds.size.height;
		CGFloat mf_height_title = mf_height_cell * 0.3;
		CGFloat mf_height_space = mf_height_cell * 0.1;
		CGFloat mf_size_image = mf_height_cell * 0.4;
		
			//图片位置
		CGFloat mf_x_image = (self.bounds.size.width - mf_size_image)/2;
		CGFloat mf_y_image = mf_height_space;
		miv.frame = CGRectMake(mf_x_image, mf_y_image, mf_size_image, mf_size_image);
		
			//标题
		CGFloat mf_x_title = 0;
		CGFloat mf_y_title = mf_height_space + mf_size_image + mf_height_space;
		mlbl_title.frame = CGRectMake(mf_x_title,mf_y_title, self.bounds.size.width, mf_height_title);
		mlbl_title.text = model->mstr_title;
		
		
		mimage_selected = [self imageWithName:model->mstr_imageName_selected];
		mimage_unselected = [self imageWithName:model->mstr_imageName_unselected];
	}
	
	Prefix(CvcIlStyle) * style = nil;
	if(model.isSelected) {
		style = model->mstyle_selected;
	}
	else {
		style = model->mstyle;
	}
		
	self.backgroundColor = style.cellBackgroundColor;
	self.layer.borderColor = style.borderColor.CGColor;
	self.layer.cornerRadius = style.cornerRadius;
	self.layer.borderWidth = style.borderWidth;
	
	if(style.isUseDefaultShadow) {
		self.layer.shadowColor = [UIColor blackColor].CGColor;
		self.layer.shadowRadius = 5;
		self.layer.shadowOffset = CGSizeMake(3, 3);
		self.layer.shadowOpacity = 0.8;
	}
	
	/**
	 怎么关闭设置字体的动画效果?,显示选中和未选中的style
	 */
	[UIView performWithoutAnimation:^{
		if(model.isSelected) {
			miv.image = mimage_selected;
			[mlbl_title setFont:model->mstyle_selected.titleFont];
			mlbl_title.font = model->mstyle_selected.titleFont;
			mlbl_title.textColor = model->mstyle_selected.titleColor;
		}
		else {
			miv.image = mimage_unselected;
			[mlbl_title setFont:model->mstyle.titleFont];
			mlbl_title.font = model->mstyle.titleFont;
			UIColor * titleColor = model->mstyle.titleColor;
			mlbl_title.textColor = titleColor;
		}
	}];
}

- (void)setSelected:(BOOL)selected {
	[super setSelected:selected];
	if(self.selected) {
		miv.image = mimage_selected;
	}
	else {
		miv.image = mimage_unselected;
	}
}

- (UIImage *)imageWithName:(NSString *)imageName {
	UIImage * image = nil;
#if 1
	image = [UIImage imageNamed:imageName];
#else
	image = [[UIImage alloc]initWithContentsOfFile:imageName];
#endif
	return image;
}

@end
